package com.study.talk.module.netty.handler;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.study.talk.module.entity.GroupInfo;
import com.study.talk.module.entity.GroupUserInfo;
import com.study.talk.module.entity.UserInfo;
import com.study.talk.module.netty.message.Attributes;
import com.study.talk.module.netty.message.Session;
import com.study.talk.module.netty.message.request.ApplyGroupRequest;
import com.study.talk.module.netty.message.response.ApplyGroupResponse;
import com.study.talk.module.netty.message.response.ApplyGroupSelfResponse;
import com.study.talk.module.service.*;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 申请加入群聊消息处理
 */

@Component
@Slf4j
@ChannelHandler.Sharable
public class ApplyGroupRequestHandler extends SimpleChannelInboundHandler<ApplyGroupRequest> {

    @Resource
    private UserService userService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private GroupUserInfoService groupUserInfoService;

    @Resource
    private GroupInfoService groupInfoService;

    @Resource
    private ApplyJoinGroupRecordService applyJoinGroupRecordService;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ApplyGroupRequest msg) throws Exception {
        log.info("申请加入群聊事件,{}", msg);
        ApplyGroupResponse applyGroupResponse = new ApplyGroupResponse();

        Session session = ctx.channel().attr(Attributes.SESSION).get();

        ApplyGroupSelfResponse applyGroupSelfResponse = new ApplyGroupSelfResponse();
        applyGroupSelfResponse.setGroupId(msg.getGroupId());
        applyGroupSelfResponse.setDateTime(msg.getDateTime());

        GroupInfo groupInfo = groupInfoService.getById(msg.getGroupId());
        if (ObjectUtils.isNull(groupInfo)) {
            applyGroupSelfResponse.setSuccess(false);
            applyGroupSelfResponse.setGroupId(msg.getGroupId());
            applyGroupSelfResponse.setReason("申请加入的群聊不存在！");
            applyGroupSelfResponse.setDateTime(msg.getDateTime());
            ctx.channel().writeAndFlush(applyGroupSelfResponse);
            return;
        }

        if (applyJoinGroupRecordService.hasRecord(groupInfo.getId(),session.getUserId())){ //已经提交过申请
            applyGroupSelfResponse.setSuccess(false);
            applyGroupSelfResponse.setReason("申请已经存在");
            userService.sendMessage(session.getUserId(), applyGroupSelfResponse);
            return;
        }

        applyJoinGroupRecordService.addRecord(groupInfo,session.getUserId()); //保存申请记录

        applyGroupSelfResponse.setSuccess(true);
        userService.sendMessage(session.getUserId(), applyGroupSelfResponse); //告诉自己申请发送成功

        //发送消息给群主有人要加入群聊
        Integer userId = session.getUserId();
        UserInfo userInfo = userInfoService.getById(userId);
        Integer mainUser = groupInfo.getMainUser();
        applyGroupResponse.setApplyUserId(userId);
        applyGroupResponse.setApplyUserName(userInfo.getNickName());
        applyGroupResponse.setGroupId(msg.getGroupId());
        applyGroupResponse.setGroupName(groupInfo.getName());
        userService.sendMessage(mainUser, applyGroupResponse);
    }
}
